/*
 * @Descripttion:
 * @Author: liuyu
 * @Date: 2023-12-04 15:44:23
 * @LastEditors: liuyu
 * @LastEditTime: 2023-12-04 15:44:23
 * @FilePath: /app/service/role.js
 */
'use strict';
const Service = require('egg').Service;
const moment = require('moment');
class DictTypeService extends Service {
  // 获取详情信息
  async show(where) {
    const { app } = this;
    const res = await app.mysql.get('db1').get('dict_type', where);
    return res;
  }
  // 获取角色列表
  async index(columns = [], where = {}, page = '', page_size = '') {
    const { app } = this;
    const options = {
      columns,
      // auth role授权部分排序也要同步调整
      orders: [[ 'dict_id', 'desc' ]],
      where,
    };
    if (page && page_size) {
      options.limit = page_size * 1;
      options.offset = (page - 1) * page_size;
    }
    const res = await app.mysql.get('db1').select('dict_type', options);
    return res;
  }

  // 添加
  async create(data) {
    const { app } = this;
    data.create_time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
    const res = await app.mysql.get('db1').insert('dict_type', data);
    return res;
  }
  // 修改
  async update(data, where) {
    data.update_time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss');
    console.log(data, where);
    try {
      const res = await this.app.mysql.get('db1').update('dict_type', data, {
        where,
      });
      console.log(res);
      return res.affectedRows === 1;
    } catch (e) {
      console.log(e);
    }
  }
  // 删除
  async delete(id) {
    const res = await this.app.mysql.get('db1').delete('dict_type', { dict_id: id });
    return res;
  }
}
module.exports = DictTypeService;
